package com.neusoft.neuiotms.dcam.fixed.service.impl;

import java.util.List;
import java.util.Objects;

import javax.persistence.EntityNotFoundException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.fixed.converter.ContractConverter;

import com.neusoft.neuiotms.dcam.fixed.dto.ContractDTO;
import com.neusoft.neuiotms.dcam.fixed.model.Contract;
import com.neusoft.neuiotms.dcam.fixed.repository.ContractRepository;
import com.neusoft.neuiotms.dcam.fixed.service.ContractService;

@Service
public class ContractServiceImpl implements ContractService {

    @Autowired
    private ContractRepository contractRepository;
    @Autowired
    private ContractConverter contractConverter;

    @Override
    public List<ContractDTO> findAll() {
        final List<Contract> models = this.contractRepository.findAll();
        return this.contractConverter.toListDTO(models);
    }

    @Override
    public Page<ContractDTO> search(final Pageable pageable, final Searchable searchable) {
        final Page<Contract> result = this.contractRepository.search(pageable, searchable);
        return this.contractConverter.toPageDTO(result);
    }

    @Override
    public ContractDTO create(final ContractDTO dto) {

        // 校验编码重复性
        final Contract check = this.contractRepository.findByCode(dto.getCode());
        if (check != null) {
            throw new UnitedException("合同号不能重复", new Object[] {});
        }

        // 创建合同
        final Contract model = this.contractConverter.toModel(dto);
        this.contractRepository.save(model);

        // 创建合同项
//        dto.getItems().stream().map(k -> {
//            k.setContractId(model.getId());
//            return this.contractItemService.create(k);
//        }).forEach(result.getItems()::add);

        return this.contractConverter.toDTO(model);
    }

    @Override
    public ContractDTO details(final Long id) {
        final Contract model;
        try {
            model = this.contractRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, id);
        }

        return this.contractConverter.toDTO(model);
    }

    @Override
    public void delete(final Long id) {


        try {
            // 删除合同项
//            model.getItems().forEach(u -> {
//                this.contractItemService.delete(u.getId());
//            });

            // 删除合同
            this.contractRepository.deleteById(id);

        } catch (final Exception e) {
            throw new UnitedException("此合同已经生成业务数据，不能删除", id);
        }

    }

    @Override
    public ContractDTO update(final Long id, final ContractDTO dto) {

        // 校验编码重复性
        final Contract check = this.contractRepository.findByCode(dto.getCode());
        if ((check != null) && (!Objects.equals(check.getId(), id))) {
            throw new UnitedException("合同号不能重复", new Object[] {});
        }

        Contract model;
        try {
            model = this.contractRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, id);
        }

        // final List<Long> items = model.getItems().stream().map(k -> k.getId()).collect(Collectors.toList());

        dto.setId(model.getId());
        model = this.contractConverter.toModel(dto);

        // 更新合同
        // model.getItems().clear();
        this.contractRepository.save(model);

        //        // 合同项
//        dto.getItems().stream().map(k -> {
//            k.setContractId(id);
//            if (null != k.getId()) {
//                items.remove(k.getId());
//                return this.contractItemService.update(k.getId(), k);
//            } else {
//                return this.contractItemService.create(k);
//            }
//        }).forEach(result.getItems()::add);

//        // 删除去掉的合同详细
//        if ((items != null) && (items.size() > 0)) {
//            // 删除合同项
//            items.forEach(u -> {
//                this.contractItemService.delete(u);
//            });
//        }
        return this.contractConverter.toDTO(model);
    }

}
